//
//  UIViewController+Alert.m
//  Fangxiaov
//
//  Created by safiri on 2017/5/5.
//  Copyright © 2017年 com.f. All rights reserved.
//

#import "UIViewController+Alert.h"

@implementation UIViewController (Alert)
- (void)alertTitle:(NSString *)title Message:(NSString *)message AlertActionTitle:(NSArray <NSString *> *)titles  Action:(void(^)(NSInteger actionTag))actionBlock {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    for (NSInteger i=0; i<titles.count; i++) {
        [alert addAction:[UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (actionBlock){actionBlock(i);}
        }]];
    }
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)alertTitle:(NSString *)title Message:(NSString *)message AlertActionTitle:(NSArray <NSString *> *)titles AlertActionStyles:(NSArray <NSNumber *>*)styles  Action:(void(^)(NSInteger actionTag))actionBlock {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    for (NSInteger i=0; i<titles.count; i++) {
        [alert addAction:[UIAlertAction actionWithTitle:titles[i] style:styles[i].integerValue handler:^(UIAlertAction * _Nonnull action) {
            if (actionBlock){actionBlock(i);}
        }]];
    }
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)alertTitle:(NSString *)title Message:(NSString *)message Action:(void(^)(BOOL yesOrCancel))actionBlock {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(YES);}
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(NO);}
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)alertTitle:(NSString *)title Message:(NSString *)message YesDestructiveActionTitle:(NSString *)yesDesTitle NoCancelActionTitle:(NSString *)noCancelTitle Action:(void(^)(BOOL yesOrCancel))actionBlock {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:yesDesTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(YES);}
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:noCancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(NO);}
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)alertTitle:(NSString *)title Message:(NSString *)message YesDefaultActionTitle:(NSString *)yesDesTitle NoDestructiveCancelActionTitle:(NSString *)noCancelTitle Action:(void(^)(BOOL yesOrCancel))actionBlock {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:yesDesTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(YES);}
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:noCancelTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(NO);}
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)alertConfirmTitle:(NSString *)title Message:(NSString *)message ConfirmAction:(void(^)(void))confirmBlock {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (confirmBlock){confirmBlock();}
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)alertTitle:(NSString *)title Message:(NSString *)message Action:(void(^)(BOOL yesOrCancel ,NSString *result))actionBlock Config:(void (^)(UITextField *textField))configTextField {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){actionBlock(NO,nil);}
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (actionBlock){
            UITextField *tf = alert.textFields.firstObject;
            actionBlock(YES,tf.text);
        }
    }]];
    [alert addTextFieldWithConfigurationHandler:configTextField];
    [self presentViewController:alert animated:YES completion:^{}];
}

- (void)alertActionSheetTitle:(NSString *)title Message:(NSString *)message selectSourceTitleArray:(NSArray<NSString *> *)dataArray actionStyles:(NSArray <NSNumber *>*)styles Selected:(void(^)(NSString *selectStr, NSInteger selectTag))selectionBlock{
    NSAssert(dataArray.count == styles.count, @"标题和样式数组元素数要相等");
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
    for (NSInteger i=0; i<dataArray.count; i++) {
        NSString *title = dataArray[i];
        NSNumber *styleNumber = styles[i];
        [alert addAction:[UIAlertAction actionWithTitle:title style:styleNumber.integerValue handler:^(UIAlertAction * _Nonnull action) {
            if (selectionBlock) {
                selectionBlock(title,i);
            }
        }]];
    }
    
    [self presentViewController:alert animated:YES completion:^{}];
}


- (void)alertAskForCallWithPhoneNumber:(NSString *)phoneNumber hintTitle:(NSString *)hintTitle message:(NSString *)message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:hintTitle message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSString *phoneNumString=[[NSString alloc] initWithFormat:@"tel:%@",phoneNumber];
        if (@available(iOS 10.2, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumString] options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumString]];
        }
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)alertAskForCallWithPhoneNumber:(NSString *)phoneNumber {
    NSString *phoneNumString = [[NSString alloc] initWithFormat:@"tel:%@",phoneNumber];
    if (@available(iOS 10.2, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumString] options:@{} completionHandler:nil];
    } else {
        [self alertAskForCallWithPhoneNumber:phoneNumber hintTitle:phoneNumber message:nil];
    }
}

- (void)alertSendValidCodeTitle:(NSString *)title Message:(NSString *)message AlertActionTitle:(NSArray <NSString *> *)titles AlertActionStyles:(NSArray <NSNumber *>*)styles  Action:(void(^)(NSInteger actionTag))actionBlock phoneTFConfig:(void (^)(UITextField *textField))phoneTFConfig validcodeTFConfig:(void (^)(UITextField *textField))validcodeTFConfig {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    for (NSInteger i=0; i<titles.count; i++) {
        [alert addAction:[UIAlertAction actionWithTitle:titles[i] style:styles[i].integerValue handler:^(UIAlertAction * _Nonnull action) {
            if (actionBlock){actionBlock(i);}
        }]];
    }
    [alert addTextFieldWithConfigurationHandler:phoneTFConfig];
    [alert addTextFieldWithConfigurationHandler:validcodeTFConfig];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
